package com.gin.no_gay_alliance.adpter.topic;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gin.common.utils.TimeAgo;
import com.gin.nga.enums.EntranceType;
import com.gin.nga.enums.TitleColor;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.databinding.ItemTopicRecentBinding;
import com.gin.no_gay_alliance.listener.ListenerUtils;
import com.gin.no_gay_alliance.utils.ReplyStatusUtils;
import com.gin.no_gay_alliance.viewmodel.TopicItem;

import java.util.List;

/**
 * 主题项适配器(最近主题)
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/24 11:12
 **/
public class TopicItemRecentAdapter extends BaseBindingRecyclerViewAdapter<ItemTopicRecentBinding, TopicItem> implements View.OnClickListener, View.OnLongClickListener {
    public TopicItemRecentAdapter(Context context) {
        super(context);
    }

    public TopicItemRecentAdapter(@NonNull Context context, @NonNull List<TopicItem> data) {
        super(context, data);
    }

    @Override
    protected void onBindViewHolder(ViewHolder<ItemTopicRecentBinding> holder, @NonNull ItemTopicRecentBinding b, int position, TopicItem item) {
        // 绑定item到根布局
        b.layoutParent.setTag(item);

        // 主题状态
        ReplyStatusUtils.configTopic(item.mirror, item.replyOnce, item.status, b.layoutTopicStatus, context);
        // 标记入口类型
        markEntranceType(item, b.layoutTopicStatus, context);
        // 主题标题
        b.tvTitle.setText(item.title);
        // 发布时间
        b.tvPostTime.setText(TimeAgo.ago(item.postDateTime));
        // 设置点击事件
        b.layoutParent.setOnClickListener(this);
        b.layoutParent.setOnLongClickListener(this);
    }

    @NonNull
    @Override
    protected ItemTopicRecentBinding buildBinding(LayoutInflater inflater) {
        return ItemTopicRecentBinding.inflate(inflater);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.layout_parent) {
            ListenerUtils.onClickTopicItem((TopicItem) v.getTag(), context);
        }

    }

    @Override
    public boolean onLongClick(View v) {
        //长按以最大页数打开
        if (v.getId() == R.id.layout_parent) {
            ListenerUtils.onLongClickTopicItem((TopicItem) v.getTag(), context);
            return true;
        }
        return false;
    }


    public static void markEntranceType(TopicItem item, ViewGroup viewGroup, @NonNull Context context) {
        if (item.entranceType == EntranceType.FORUM) {
            TextView textView = new TextView(context);
            textView.setText("[版面]");
            textView.setTextColor(Color.parseColor("#" + TitleColor.RED.rgb));
            viewGroup.addView(textView);
            viewGroup.setVisibility(View.VISIBLE);
        }
    }
}
